package com.jianganming.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelDemo {
    public static void main(String[] args)throws Exception {
        test();
    }

    private static void copyFile2()throws Exception{
        File file = new File("/Users/jianganming/Downloads/016-尚硅谷-Netty核心技术及源码剖析-Channel拷贝文件.avi");
        FileInputStream in = new FileInputStream(file);
        FileChannel read = in.getChannel();
        //输出流
        FileOutputStream out = new FileOutputStream("/Users/jianganming/Downloads/江安明简历03.avi");
        //文件通道
        FileChannel write = out.getChannel();

        write.transferFrom(read,0,read.size());

        in.close();

        out.close();
    }

    private static void copyFile()throws Exception{
        File file = new File("/Users/jianganming/Downloads/016-尚硅谷-Netty核心技术及源码剖析-Channel拷贝文件.avi");
        FileInputStream in = new FileInputStream(file);
        FileChannel channel = in.getChannel();
        //输出流
        FileOutputStream out = new FileOutputStream("/Users/jianganming/Downloads/江安明简历02.avi");
        //文件通道
        FileChannel channel2 = out.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        while (true){
            byteBuffer.clear();
            int read = channel.read(byteBuffer);
            if(read==-1){
                break;
            }
            byteBuffer.flip();
            channel2.write(byteBuffer);
        }

        in.close();
        out.close();
    }

    private static void readFile()throws Exception{
        File file = new File("/Users/jianganming/Downloads/file01.txt");
        FileInputStream in = new FileInputStream(file);

        FileChannel channel = in.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());

        channel.read(byteBuffer);

        byteBuffer.flip();

        /*byte[] bytes = new byte[byteBuffer.capacity()];
        int i=0;
        while (byteBuffer.hasRemaining()){
            bytes[i++]=byteBuffer.get();
        }
        System.out.println(new String(bytes));*/
        System.out.println(new String(byteBuffer.array()));

        in.close();
    }

    private static void writeFile()throws Exception{
        String str = "我爱你中国";
        //输出流
        FileOutputStream out = new FileOutputStream("/Users/jianganming/Downloads/file01.txt");
        //文件通道
        FileChannel channel = out.getChannel();
        //字节缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //向缓冲区写入数据
        byteBuffer.put(str.getBytes());
        //指向缓冲区数组的0下标
        byteBuffer.flip();
        //将缓冲区写入通道
        channel.write(byteBuffer);

        out.close();
    }

    public static void test()throws Exception{
        RandomAccessFile aFile = new RandomAccessFile("test.txt", "rw");
        FileChannel inChannel = aFile.getChannel();
        ByteBuffer buf = ByteBuffer.allocate(1024);
        int bytesRead = 0;
        while ((bytesRead=inChannel.read(buf)) != -1) {
            buf.flip();//重新指向开始位置
            System.out.println(new String(buf.array(),0,bytesRead));
            buf.clear();//清除缓冲区
        }
        aFile.close();
    }
}
